package com.tv189.springbatchtest.conditionstep;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * @ClassName: ConditionStepJob
 * @Description: 条件步骤任务
 * @Author: 19202
 * @Date: 2025/5/28 22:24
 * @Version 1.0
 **/
@SpringBootApplication
@EnableBatchProcessing
public class ConditionStepJob {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Tasklet firstTasklet() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------firstTasklet-----");
            //return RepeatStatus.FINISHED;
            throw new RuntimeException("假装失败");
        };
    }

    @Bean
    public Tasklet successTasklet() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------successTasklet-----");
            return RepeatStatus.FINISHED;
            //throw new RuntimeException("假装失败");
        };
    }

    @Bean
    public Tasklet failTasklet() {
        return (stepContribution, chunkContext) -> {
            System.out.println("------failTasklet-----");
            return RepeatStatus.FINISHED;
        };
    }

    @Bean
    public Step firstStep() {
        return stepBuilderFactory.get("firstStep")
                .tasklet(firstTasklet())
                .build();
    }

    @Bean
    public Step successStep() {
        return stepBuilderFactory.get("successStep")
                .tasklet(successTasklet())
                .build();
    }

    @Bean
    public Step failStep() {
        return stepBuilderFactory.get("failStep")
                .tasklet(failTasklet())
                .build();
    }

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job")
                .start(firstStep())
                .on("FAILED").stopAndRestart(successStep())
                //.on("FAILED").end()  直接结束任务  返回的状态为COMPLETED状态
                //.on("FAILED").fail() 直接结束任务 返回的状态为FAILED状态
                //.on("FAILED").stop() 直接结束任务 返回的状态为STOPPED状态
                //.on("FAILED").stop() 直接结束任务 返回的状态为STOPPED状态
                .from(firstStep()).on("COMPLETED").to(successStep())
                .end()
                .incrementer(new RunIdIncrementer())
                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ConditionStepJob.class, args);
    }
}
